package com.dd.cloud.user.mq;

import com.alibaba.fastjson.JSONObject;
import com.dd.cloud.common.constants.TemplateConstants;
import com.dd.cloud.user.service.news.INewsInfoService;
import com.dd.cloud.user.service.plat.TemplateMessAgeService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author xt
 */
@Slf4j
@Component
public class TemplateMessage {
    @Autowired
    private TemplateMessAgeService templateMessAgeService;
    @Autowired
    private INewsInfoService newsInfoService;

    @RabbitHandler
    @RabbitListener(queues = MQConfig.TEMPLATE_QUEUE)
    public void processHandler(Channel channel, Message message, String params) throws IOException {
        try {
            log.info("开始发送板消息,参数为{}", params);
            String[] arrayStr = params.split("-");
            if (arrayStr.length >= 2) {
                Integer type = Integer.parseInt(arrayStr[0]);
                String param = arrayStr[1];
                if (TemplateConstants.TemplateType.THREE_TYPE.equals(type)) {
//                    log.info("rabbitMQ发送处方退款模板通知");
//                    templateMessAgeService.sendPresRefundTemplateMessage(param);
                    log.info("rabbitMQ发送处方退款系统消息通知");
                    newsInfoService.sendPresRefundMessage(param);
                } else if (TemplateConstants.TemplateType.NINE_TYPE.equals(type)) {
                    log.info("rabbitMQ发送问诊预约状态模板通知");
                    templateMessAgeService.sendConsultTemplateMessage(param);
                    log.info("rabbitMQ发送预约成功系统消息");
                    newsInfoService.sendConsultSuccessMessage(param);
                } else if (TemplateConstants.TemplateType.FIVE_TYPE.equals(type)) {
                    log.info("rabbitMQ发送评价模板通知");
                    templateMessAgeService.sendEvaluateTemplateMessage(param);
                    log.info("rabbitMQ发送评价系统消息");
                    newsInfoService.sendEvaluateMessage(param);
                } else if (TemplateConstants.TemplateType.SEVEN_TYPE.equals(type)) {
//                    log.info("rabbitMQ发送处方支付成功模板通知");
//                    templateMessAgeService.sendPreSuccessBuyTemplateMessage(param);
                    log.info("rabbitMQ发送处方支付成功系统通知");
                    newsInfoService.sendPreSuccessBuyMessage(param);
                } else if (TemplateConstants.TemplateType.TWELVE_TYPE.equals(type)) {
                    log.info("rabbitMQ发送问诊退款模板通知");
                    templateMessAgeService.sendConsultRefundOrder(param);
                    log.info("rabbitMQ发送问诊退款系统通知");
                    newsInfoService.sendConsultRefundOrder(param);
                } else if (TemplateConstants.TemplateType.FOUR_TYPE.equals(type)) {
                    log.info("rabbitMQ发送问诊取消通知模板通知");
//                    templateMessAgeService.sendCancelConsultTemplateMessage(param);
                } else if (TemplateConstants.TemplateType.PRES_ORDER_UN_PAY_PAT_GZH.equals(type)) {
                    log.info("rabbitMQ发送患者公众号待支付订单模板消息通知");
                    String json = params.substring(params.indexOf("-") + 1);
                    JSONObject jsonObject = JSONObject.parseObject(json);
                    templateMessAgeService.sendPresOrderUnPayTemplateMessage(jsonObject.getString("docName"), jsonObject.getString("patName"), jsonObject.getString("medId"), jsonObject.getString("createDate"), jsonObject.getInteger("amount"), jsonObject.getString("tencode"));
                }
            }
            //手动确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            if (message.getMessageProperties().getRedelivered()) {
                // 拒绝消息
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            } else {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            }
        }
    }
}
